package ch.mse.android.starbucksfinder.net;

import java.net.URL;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import ch.mse.android.starbucksfinder.StarbucksLocation;
import ch.mse.android.starbucksfinder.StarbucksLocationHandler;

public class ServerConnector {

	public List<StarbucksLocation> downloadAndParse() throws Exception {
			/* Create a URL we want to load some xml-data from. */
			URL url = new URL("http://www.lightforce.ch/tools/mobop/data.xml");
			/* Get a SAXParser from the SAXPArserFactory. */
			SAXParserFactory spf = SAXParserFactory.newInstance();
			SAXParser sp = spf.newSAXParser();
			/* Get the XMLReader of the SAXParser we created. */
			XMLReader xr = sp.getXMLReader();
			/* Create a new ContentHandler and apply it to the XML-Reader */
			StarbucksLocationHandler handler = new StarbucksLocationHandler();
			xr.setContentHandler(handler);
			/* Parse the xml-data from our URL. */
			xr.parse(new InputSource(url.openStream()));
			/* Parsing has finished. */
			/* Our ExampleHandler now provides the parsed data to us. */
			return handler.getParsedData();
	}

}
